package cn.gov.pbc.gd.credit.web.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liu.tools.DateUtil;
import com.liu.tools.NetHelper;
import com.liu.tools.base64.Base64;
import com.liu.tools.conf.ConfigurerEx;

import cn.gov.pbc.gd.credit.common.constant.CommonConstant;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.common.util.UserUtil;
import cn.gov.pbc.gd.credit.common.vo.LoginVo;
import cn.gov.pbc.gd.credit.user.service.login.ILoginService;

@Controller
@RequestMapping("")
public class IndexController {

	@Autowired
	private IRedisService redisService;
	
	@Autowired
	private ILoginService loginService;
	
	@RequestMapping("/index")
	public String index(Model model, HttpServletRequest request){
        
		String sid = request.getParameter("sid");
		Object obj = redisService.getObject(CommonConstant.CACHE_LOGIN_PREFIX + UserUtil.getUserIdBySid(sid));
		model.addAttribute("result", obj);
		model.addAttribute("sid", sid);
		model.addAttribute("resVersion", ConfigurerEx.getProperty("res.version", DateUtil.format(new Date(), DateUtil.FORMAT_DAY)));
		String showMenu = NetHelper.getReqParams(request, "showMenu", "INDEX");
		model.addAttribute("showMenu", showMenu);
		return "/index";
	}

	@RequestMapping("/login")
	public String login(Model mv, HttpServletRequest request){
		
		mv.addAttribute("loginConf", loginService.loginConf(new LoginVo(), request).getData());
		
		return "/login";
	}
	
	@ResponseBody
	@RequestMapping("/test/img")
	public String test(Model mv, HttpServletRequest request) throws Exception {
		
		
		return encodeBase64File("d:\\2017630154826807.jpg");
	}
	
	 public static String encodeBase64File(String path) throws Exception {  
		   File file = new File(path);  
		   FileInputStream inputFile = new FileInputStream(file);  
		   byte[] buffer = new byte[(int) file.length()];  
		   inputFile.read(buffer);  
		   inputFile.close();  
		   return  Base64.encodeToString(buffer, 0);
		  
	} 
}
